﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;


namespace TestDBSourceToolsLibNUnit
{
    [TestFixture]
    public class TestZipFileWriter
    {
        private List<string> m_EventMessages = new List<string>();
        private int m_TotalFilesInZip = 0;

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            DBSourceToolsLib.UserSettings.Instance().StartupPath = Constants.StartupPath;
        }

        [Test]
        public void TestAddInvalidProjectFile()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            bool bThrew = false;
            try
            {
                oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile + "doesnotexist");
            }
            catch (Exception ex)
            {
                bThrew = true;
            }

            Assert.IsTrue(bThrew, "Invalid project file should throw");
        }


        [Test]
        public void TestAddInvalidDeploymentTargetName()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            bool bThrew = false;
            try
            {
                oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
                oZipWriter.SetDeploymentTargetNickName("invalidNick");
            }
            catch (Exception ex)
            {
                bThrew = true;
            }

            Assert.IsTrue(bThrew, "Invalid target nick file should throw");
        }

        [Test]
        public void TestAddInvalidSourceNickName()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            bool bThrew = false;
            try
            {
                oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
                oZipWriter.SetSourceDatabaseNickName("invalidNick");
            }
            catch (Exception ex)
            {
                bThrew = true;
            }

            Assert.IsTrue(bThrew, "Invalid source nick file should throw");
        }



        [Test]
        public void TestCreateBatchFile()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
            oZipWriter.SetDeploymentTargetNickName(Constants.AdventureWorksLT_1_1_DeployTargetNick);
            oZipWriter.SetSourceDatabaseNickName(Constants.AdventureWorksLT_DatabaseName);

            Assert.IsTrue(oZipWriter.WriteZipFile());

            Assert.IsTrue(System.IO.File.Exists(strZipFileName));

            using (Ionic.Zip.ZipFile oZipFile = Ionic.Zip.ZipFile.Read(strZipFileName))
            {
                Assert.Greater(oZipFile.Count, 0);
                Ionic.Zip.ZipEntry oBatchFile = oZipFile["Create_deploy_AdventureWorksLT_1.1.bat"];
                Assert.IsNotNull(oBatchFile, "Could not find Create_deploy_AdventureWorksLT_1.1.bat");

            }

        }

        [Test]
        public void TestAddSourceDatabaseDirectories()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
            oZipWriter.SetDeploymentTargetNickName(Constants.AdventureWorksLT_1_1_DeployTargetNick);
            oZipWriter.SetSourceDatabaseNickName(Constants.AdventureWorksLT_DatabaseName);
            oZipWriter.WriteZipFile();

            using (Ionic.Zip.ZipFile oZipFile = Ionic.Zip.ZipFile.Read(strZipFileName))
            {
                Assert.Greater(oZipFile.Count, 0);
                string strScriptOptions = Constants.AdventureWorksLT_DatabaseName + @"\ScriptOptions.xml";
                Ionic.Zip.ZipEntry oAdventureWorks = oZipFile[strScriptOptions];
                Assert.IsNotNull(oAdventureWorks, "Could not find " + strScriptOptions);

            }

        }

        [Test]
        public void TestAddBinDirectory()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
            oZipWriter.SetDeploymentTargetNickName(Constants.AdventureWorksLT_1_1_DeployTargetNick);
            oZipWriter.SetSourceDatabaseNickName(Constants.AdventureWorksLT_DatabaseName);
            oZipWriter.WriteZipFile();

            using (Ionic.Zip.ZipFile oZipFile = Ionic.Zip.ZipFile.Read(strZipFileName))
            {
                Assert.Greater(oZipFile.Count, 0);
                string strDBDataSourceLoader = @".\bin\DBDataSourceLoader.exe";
                Assert.IsTrue(oZipFile.EntryFileNames.Contains("bin/DBSourceDataLoader.exe"));
                
            }

            
        }

        [Test]
        public void TestWithDelegate()
        {
            m_EventMessages.Clear();

            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
            oZipWriter.SetDeploymentTargetNickName(Constants.AdventureWorksLT_1_1_DeployTargetNick);
            oZipWriter.SetSourceDatabaseNickName(Constants.AdventureWorksLT_DatabaseName);
            oZipWriter.AddProgress += new EventHandler<Ionic.Zip.AddProgressEventArgs>(oZipWriter_AddProgress);

            m_TotalFilesInZip = oZipWriter.CountFilesInZip();

            oZipWriter.WriteZipFile();
            Assert.Greater(m_EventMessages.Count, 0);

            //foreach (string strEvent in m_EventMessages)
            //    Console.WriteLine(strEvent);

        }

        void oZipWriter_AddProgress(object sender, Ionic.Zip.AddProgressEventArgs e)
        {
            switch (e.EventType)
            {
                case Ionic.Zip.ZipProgressEventType.Adding_Started:
                    m_EventMessages.Add("Adding");
                    break;
                case Ionic.Zip.ZipProgressEventType.Adding_AfterAddEntry:
                    m_EventMessages.Add(String.Format("Adding file {0}, {1}",
                        e.CurrentEntry.FileName, e.EntriesTotal.ToString()
                        ));

                    break;
                case Ionic.Zip.ZipProgressEventType.Adding_Completed:
                    m_EventMessages.Add("Done");
                    break;
            }
        }

        [Test]
        public void TestZipWriterWithProgress()
        {
            m_EventMessages.Clear();

            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddDeployTarget.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
            oZipWriter.SetDeploymentTargetNickName(Constants.AdventureWorksLT_1_1_DeployTargetNick);
            oZipWriter.SetSourceDatabaseNickName(Constants.AdventureWorksLT_DatabaseName);
            System.Windows.Forms.Form oForm = new System.Windows.Forms.Form();
            oZipWriter.Execute(oForm);

            m_TotalFilesInZip = oZipWriter.CountFilesInZip();

            //foreach (string strEvent in m_EventMessages)
            //    Console.WriteLine(strEvent);

        }

        [Test]
        public void TestAddProjectXML()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testAddProjectXML.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
            oZipWriter.SetDeploymentTargetNickName(Constants.AdventureWorksLT_1_1_DeployTargetNick);
            oZipWriter.SetSourceDatabaseNickName(Constants.AdventureWorksLT_DatabaseName);
            oZipWriter.WriteZipFile();

            using (Ionic.Zip.ZipFile oZipFile = Ionic.Zip.ZipFile.Read(strZipFileName))
            {
                Assert.Greater(oZipFile.Count, 0);
                Assert.IsTrue(oZipFile.EntryFileNames.Contains(Constants.AdventureWorks_DatabaseXmlFile));
            }


        }

        [Test]
        public void TestNoHiddenDirectories()
        {
            string strZipFileName = Constants.TestFiles_Output_Zip_Dir + "testNoHiddenDirectories.zip";
            DBSourceToolsLib.ZipWriter oZipWriter = new DBSourceToolsLib.ZipWriter(
                strZipFileName);

            oZipWriter.SetProjectFile(Constants.AdventureWorks_ProjectFile);
            oZipWriter.SetDeploymentTargetNickName(Constants.AdventureWorksLT_1_1_DeployTargetNick);
            oZipWriter.SetSourceDatabaseNickName(Constants.AdventureWorksLT_DatabaseName);
            oZipWriter.WriteZipFile();

            using (Ionic.Zip.ZipFile oZipFile = Ionic.Zip.ZipFile.Read(strZipFileName))
            {
                Assert.Greater(oZipFile.Count, 0);
                Assert.IsTrue(oZipFile.EntryFileNames.Contains(Constants.AdventureWorks_DatabaseXmlFile));
            }


        }



    }
}
